<?php

namespace app\controller\api;

use app\model\api\YysHero;
use laytp\controller\Api;
use think\exception\ValidateException;
/**
 * Class Yys
 * @package app\index\controller
 */
class Yys extends Api
{
    protected $model;

    protected function initialize()
    {
        $this->model = new YysHero();
    }


    /**
     * 角色列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        $data = input('post.');
        $keyword = !empty($data['keyword'])?$data['keyword']:'';
        $category = !empty($data['category'])?$data['category']:'';
        $page  = !empty($data['page'])?$data['page']:1;

        try{
            //验证器
            validate(\app\validate\api\Yys::class)
                ->scene('list')
                ->check([
                    'page' => $page,
                ]);

            $list = $this->model->list($keyword,$category);
            return $this->success('success',$list['data'],array(
                'total' => $list['total'],
                'current_page' => $list['current_page'],
                'last_page' => $list['last_page'],
                'per_page' => $list['per_page'],
            ));

        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }


    /**
     * 角色详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(){
        $data = input('post.');
        $role_id = !empty($data['role_id'])?$data['role_id']:'';
        try{
            //验证器
            validate(\app\validate\api\Yys::class)
                ->scene('detail')
                ->check([
                    'role_id' => $role_id
                ]);

            $detail = $this->model->detail($role_id);
            return $this->success('success',$detail);
        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

}
